package com.chengliang.bbs.service.impl;

import com.chengliang.bbs.entry.UserAuths;
import com.chengliang.bbs.mapper.UserAuthsMapper;
import com.chengliang.bbs.service.UserAuthsService;
import com.chengliang.bbs.util.StringUtil;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @author: chengliang
 * @create: 2019-07-28 22:22
 */
@Service
public class UserAuthsServiceImpl implements UserAuthsService {

    private final UserAuthsMapper userAuthsMapper;

    private final PasswordEncoder passwordEncoder;

    public UserAuthsServiceImpl(UserAuthsMapper userAuthsMapper, PasswordEncoder passwordEncoder) {
        this.userAuthsMapper = userAuthsMapper;
        this.passwordEncoder = passwordEncoder;
    }

    @Override
    public UserAuths getUserAuths(String userIdentifier) {
        if(StringUtil.isEmpty(userIdentifier)){
            return null;
        }
        return userAuthsMapper.selectUserByUserIdentifier(userIdentifier);
    }

    @Override
    public boolean updateIfverifiedByIdentifier(String identifier, Integer ifverified) {
        return userAuthsMapper.updateIfverifiedByIdentifier(identifier,ifverified);
    }

    @Override
    public boolean updatePassowrdByUserId(Integer userId, String newPassword) {
        if(StringUtil.isEmpty(userId)){
            return false;
        }
        return userAuthsMapper.updatePasswordByUserId(userId, passwordEncoder.encode(newPassword));
    }
}
